package com.sunyard.pcc.shiro.interceptor;

import com.sunyard.pcc.common.bo.SysMenuBO;
import com.sunyard.pcc.common.bo.SysUserCacheBO;
import com.sunyard.pcc.common.constant.ErrorCodeEnum;
import com.sunyard.pcc.common.constant.IsSuperAdminEnum;
import com.sunyard.pcc.common.exception.PccCommonException;
import com.sunyard.pcc.common.shiro.ShiroUtils;
import com.sunyard.pcc.core.service.MenuService;
import com.sunyard.pcc.core.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author: Hewei
 * @Description: 权限管理配置拦截器
 * @Date: Created in 14:31 2018/3/1
 */
@Slf4j
@Component
public class ShiroAuthInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @Autowired
    private RedisTemplate redisTemplate;


    //进行权限判断
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String path = request.getServletPath();
        log.info(path);
        //特定path不进行拦截
        if (ShiroUtils.urlFilter(path)){
            return true;
        }
        //用户菜单权限验证
        SysUserCacheBO user = ShiroUtils.getCacheUser();
        if (IsSuperAdminEnum.ISDELETED.getStatus().equals(user.getIsSuperAdmin())){
            return true;
        }
        List<SysMenuBO> menus = user.getSysMenuBOS();
        SysMenuBO currentMenu = new SysMenuBO();
        currentMenu.setUrl(path)
                   .setPerms(request.getMethod());

        Boolean hasPermission = menuService.hasPermissionToMenu(currentMenu, menus);
        //没有访问权限
        if (!hasPermission){
            throw new PccCommonException(ErrorCodeEnum.DO_NOT_HAS_PERMISSION);
        }

        return super.preHandle(request, response, handler);
    }


    /**
     * 用户登录后进行用户权限配置
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        String path = request.getServletPath();
        //不为登录url 则return
        if (!path.equals(ShiroUtils.LOGIN_URL) && request.getMethod().equals(RequestMethod.POST.name())){
            return;
        }

        if (ShiroUtils.getUser() != null){
            //首次登陆设置用户权限
            log.info(ShiroUtils.getUser().getTelephone() + "用户登录成功， 设置用户权限");
            SysUserCacheBO sysUserCacheBO = userService.selectUserAndRolesByTelephone(ShiroUtils.getUser().getTelephone());
            //将用户及权限信息存入缓存
            ShiroUtils.setUserCacheIntoSession(sysUserCacheBO);
        }

    }
}
